package com.zby.admin.service.imp;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zby.admin.mapper.CategoryMapper;
import com.zby.admin.pojo.Category;
import com.zby.admin.pojo.dto.CategoryDto;
import com.zby.admin.service.CategoryService;
import com.zby.utils.PageResult;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImp extends ServiceImpl<CategoryMapper,Category> implements CategoryService {
    @Override
    public void insert(CategoryDto categoryDto) {
        System.out.println(categoryDto.getCategoryName());
        if(categoryDto.getCategoryName()== null){
            throw new RuntimeException("请输入分类名称");
        }

        Category category = new Category();
        category.setCategoryName(categoryDto.getCategoryName());
        this.save(category);
    }

    @Override
    public  Page<Category>  selectList(Integer page, Integer pageSize) {
        System.out.println("page:"+page+"pageSize:"+pageSize);

        Page<Category> categoryPage = new Page<>(page, pageSize);

        return this.page(categoryPage);
    }

    @Override
    public boolean update(CategoryDto categoryDto) {
        Category category = new Category();
        category.setId(categoryDto.getId());
        category.setCategoryName(categoryDto.getCategoryName());
        UpdateWrapper<Category> categoryDtoUpdateWrapper = new UpdateWrapper<>();
        UpdateWrapper<Category> set = categoryDtoUpdateWrapper.eq("id", category.getId())
                .set("category_name", category.getCategoryName());
        return this.update(set);

    }
}
